package cn.mrlong.base.utils.recordphone;

import android.media.MediaRecorder;
import android.os.Environment;

import java.io.File;

import cn.mrlong.base.utils.FileUtils;


//录音工具类
public class RecordAudioUtil {
    private MediaRecorder mediaRecorder = null; //媒体录制
    String recDir = "CallRec";                  //保存目录
    private File recAudioSaveFileDir = null;   //文件保存目录
    private boolean sdCardExists = false;      //判断SD卡是否存在
    private boolean isRec = false;                //判断录音状态
    File recAudioSaveFile = null;//生成的文件
    String recAudioSaveFileName = null;//生成的文件路径
    public static final String AUDIO_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "ALinkAudio" + File.separator;
    private RecordListener recordListener;

    public RecordAudioUtil(RecordListener recordListener) {
        this.recordListener = recordListener;
        if (this.sdCardExists = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {  //判断SD卡是否存在
            this.recAudioSaveFileDir = new File(AUDIO_PATH);    //保存录音目录
            if (!this.recAudioSaveFileDir.exists()) {
                this.recAudioSaveFileDir.mkdir();           //如果父目录不存在创建目录.
            }
        }
    }

    public File record() {
        checkSaveFileSize();
        if (this.sdCardExists) {
            recAudioSaveFileName = this.recAudioSaveFileDir.toString() + File.separator + "" + System.currentTimeMillis() + ".3gp";
            recAudioSaveFile = new File(recAudioSaveFileName);
            if (null == mediaRecorder)
                this.mediaRecorder = new MediaRecorder();
            this.mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
            this.mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//内容输出格式
            this.mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//音频编码方式
            this.mediaRecorder.setOutputFile(recAudioSaveFileName);
            try {
                this.mediaRecorder.prepare();
            } catch (Exception e) {
                e.printStackTrace();
            }
            this.mediaRecorder.start();
            this.isRec = true;
        }
        return recAudioSaveFile;
    }

    /**
     * 检查文件存储的大小
     */
    private void checkSaveFileSize() {
        try {
            File file = new File(AUDIO_PATH);
            if (!file.exists()) {
                file.mkdirs();
            }
            double fileSize = FileUtils.INSTANCE.getAutoFileOrFilesSize(AUDIO_PATH, 3);
            if (fileSize > 0) {
                File[] array = file.listFiles();//获得该文件夹内的所有文件
                //如果大于100mb 就全部删除
                if (fileSize > 100) {
                    for (File file1 : array) {
                        file1.delete();
                    }
                }
                //如果文件超过20个就删除 时间最早的一个
                if (array != null && array.length >= 20) {
                    //Arrays.sort(array);
                    array[0].delete();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void stop() {
        if (this.isRec) {
            if (null != recordListener
                    && null != recAudioSaveFileName
                    && !"".equals(recAudioSaveFileName)) {
                recordListener.returnSuccFilePath(recAudioSaveFileName, FileUtils.INSTANCE.FormetFileSize(recAudioSaveFileName.length(),FileUtils.INSTANCE.SIZETYPE_MB) + "");
            }
            this.mediaRecorder.stop();
            this.mediaRecorder.release();
            this.mediaRecorder = null;
        }
    }
}
